package thread;

import java.util.concurrent.atomic.AtomicInteger;

public class leetCode1114_2 {
    private AtomicInteger a = new AtomicInteger();
    private AtomicInteger b = new AtomicInteger();
    private final Object lock = new Object();

    public void first(Runnable printFirst) throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.
        synchronized (lock) {
            printFirst.run();
            a.incrementAndGet();
            lock.notifyAll();
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {
        synchronized (lock) {
            if(a.get() != 1){
                lock.notifyAll();
            }
            // printSecond.run() outputs "second". Do not change or remove this line.
            printSecond.run();
            b.incrementAndGet();
        }
    }

    public void third(Runnable printThird) throws InterruptedException {
        synchronized (lock) {
            if (b.get() != 1) {
                lock.notifyAll();
            }
            // printThird.run() outputs "third". Do not change or remove this line.
            printThird.run();
        }
    }
}
